package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.entity.dto.CusDeviceCapacityPageParam;
import com.xinsoft.entity.po.CusDeviceCapacity;
import com.xinsoft.entity.po.SysDictionary;
import com.xinsoft.entity.po.SysMaterial;
import com.xinsoft.entity.po.SysUser;
import com.xinsoft.entity.vo.CusDeviceCapacityVo;
import com.xinsoft.mapper.CusDeviceCapacityMapper;
import com.xinsoft.service.CusDeviceCapacityService;
import com.xinsoft.service.SysDictionaryService;
import com.xinsoft.service.SysMaterialService;
import com.xinsoft.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author hsm
* @description 针对表【cus_device_capacity(设备产能)】的数据库操作Service实现
* @createDate 2023-06-16 16:00:06
*/
@Service
public class CusDeviceCapacityServiceImpl extends ServiceImpl<CusDeviceCapacityMapper, CusDeviceCapacity>
    implements CusDeviceCapacityService{

    @Resource
    public SysMaterialService sysMaterialService;

    @Resource
    public SysDictionaryService sysDictionaryService;

    @Resource
    public SysUserService sysUserService;


    @Override
    public CusDeviceCapacity addCusDeviceCapacity(CusDeviceCapacity cusDeviceCapacity) {
        //校验关联产品产能是否重复添加
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id",cusDeviceCapacity.getDeviceId());
        queryWrapper.eq("material_id",cusDeviceCapacity.getMaterialId());
        List list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)){
            throw new BDException("操作失败，此产品已经关联了本设备，请勿重复关联！");
        }
        save(cusDeviceCapacity);

        return cusDeviceCapacity;
    }

    @Override
    public CusDeviceCapacity editCusDeviceCapacity(CusDeviceCapacity cusDeviceCapacity) {

        //校验关联产品产能是否重复添加
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id",cusDeviceCapacity.getDeviceId());
        queryWrapper.eq("material_id",cusDeviceCapacity.getMaterialId());
        queryWrapper.ne("id",cusDeviceCapacity.getId());
        List list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)){
            throw new BDException("操作失败，此产品已经关联了本设备，请勿重复关联！");
        }
        updateById(cusDeviceCapacity);

        return cusDeviceCapacity;
    }

    @Override
    public CusDeviceCapacityVo getCusDeviceCapacity(Integer id) {
        CusDeviceCapacityVo result = new CusDeviceCapacityVo();
        CusDeviceCapacity entity = getById(id);
        BeanUtils.copyProperties(entity,result);
        result.setId(entity.getId());

        //产品
        SysMaterial material = sysMaterialService.getById(result.getMaterialId());
        result.setMaterialName(material.getMaterialName());
        result.setMaterialSpecific(material.getMaterialSpecific());
        result.setMaterialNo(material.getMaterialNo());

        SysDictionary unit =  sysDictionaryService.getById(material.getMainUnit());
        result.setMaterialMainUnitName(unit.getDicName());

        SysUser user = sysUserService.getById(entity.getCreateBy());
        result.setCreateByName(user.getRealName());

        return result;
    }

    @Override
    public void del(List<Integer> ids) {
        removeByIds(ids);
    }

    @Override
    public IPage<CusDeviceCapacityVo> findPageListByParam(CusDeviceCapacityPageParam cusDeviceCapacityPageParam) {
        //分页
        Page<CusDeviceCapacityVo> page = new Page<>(cusDeviceCapacityPageParam.getCurrentPage(), cusDeviceCapacityPageParam.getPageSize());
        //查询分页
        IPage<CusDeviceCapacityVo> pageList = this.baseMapper.findListByParam(page, cusDeviceCapacityPageParam);

        return pageList;
    }
}




